/**
 * Created by Administrator on 2017/3/2 0002.
 */
var Page5={
    create:function () {
    var page={};

        var t0,t1;
        var tw_whole,tw_t0,tw_t1,tw_click;
        page.ui=new PIXI.DisplayObjectContainer();

        var bg=new PIXI.Sprite(resource['assets/images/p5_bg.png'].texture);
        page.ui.addChild(bg);

        var t0=new PIXI.Sprite(resource['assets/images/p5_t0.png'].texture);
        t0.x=393;
        t0.y=105;
        t0.alpha=0;
        t0.anchor.set(0.5);
        page.ui.addChild(t0);

        var t1=new PIXI.Sprite(resource['assets/images/p5_t1.png'].texture);
        t1.x=290;
        t1.y=185;
        t1.alpha=0;
        t1.anchor.set(0.5);
        page.ui.addChild(t1);

        var arr_texture=[];
        for(var i=1;i<25;i++)
        {
            if(i<10)
            {
                var texture = resource['assets/images/click000'+i.toString()+'.png'].texture;
            }
            else
            {
                var texture = resource['assets/images/click00'+i.toString()+'.png'].texture;
            }
            arr_texture.push(texture);

        }
        var sp_click=new PIXI.extras.AnimatedSprite(arr_texture);
        sp_click.anchor.set(0.5);
        sp_click.x=432;
        sp_click.y=319;
        sp_click.animationSpeed=0.4;
        sp_click.alpha=0;
        sp_click.gotoAndPlay(1);
        page.ui.addChild(sp_click);
        tw_click=new TWEEN.Tween({alpha:0,scale:0})
            .to({alpha:1,scale:1},1000)
            .onUpdate(function () {
                sp_click.scale.x=this.scale;
                sp_click.scale.y=this.scale;
                sp_click.alpha=this.alpha;
            })
            .delay(300)
            .easing(TWEEN.Easing.Back.Out)
        // var sp_click=new PIXI.Sprite(resource['assets/images/tip_click.png'].texture);
        // sp_click.anchor.set(0.5,1);
        // sp_click.x=657;
        // sp_click.y=177;
        // sp_click.skew.x=0.7;
        // sp_click.alpha=0;
        // page.ui.addChild(sp_click);
        // tw_click=new TWEEN.Tween({skew:0.7,alpha:0.5,scale:0.9})
        //     .to({skew:0,alpha:1,scale:1},500)
        //     .onUpdate(function () {
        //         sp_click.skew.x=this.skew;
        //         sp_click.alpha=this.alpha;
        //         sp_click.scale.x=this.scale;
        //         sp_click.scale.y=this.scale;
        //     })
        //     .delay(300)
        //     .easing(TWEEN.Easing.Back.InOut)
        //     .yoyo(true)
        //     .repeat(Infinity)
            // .start();

        var click=new PIXI.Graphics();
        click.beginFill(0x0);
        click.drawRect(0,0,100,275);
        click.x=386;
        click.y=200;
        click.alpha=0;

        page.ui.addChild(click);
        click.on('pointerdown',downHandler);
        click.on('pointerup',upHandler);

        page.In=function () {
            if(tw_whole)
            {
                tw_whole.stop();
                TWEEN.remove(tw_whole);
            }

            tw_t0=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(3000+100)
                .onUpdate(function () {
                    t0.alpha=this.alpha;
                    t0.scale.x=this.scale;
                    t0.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            tw_t1=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(3000+300)
                .onUpdate(function () {
                    t1.alpha=this.alpha;
                    t1.scale.x=this.scale;
                    t1.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            setTimeout(function () {
                tw_click.start();
                click.interactive=true;
            },4000)


        }
        page.Out=function () {
            if(tw_t0)
            {
                tw_t0.stop();
            }
            if(tw_t1)
            {
                tw_t1.stop();
            }


            tw_whole=new TWEEN.Tween({alpha:1})
                .to({alpha:0},400)
                .onUpdate(function () {
                    page.ui.alpha=this.alpha;
                })
                .onComplete(function () {
                    page.ui.alpha=1;
                    t0.alpha=0;
                    t1.alpha=0;
                })
                .easing(TWEEN.Easing.Cubic.InOut)
                .start();
        }

        var downPT={x:0,y:0};
        function upHandler(e) {
            var dis=(e.data.global.x-downPT.x)*(e.data.global.x-downPT.x)-(e.data.global.y-downPT.y)*(e.data.global.y-downPT.y)
            if(dis<25)
            {
                console.log('click');
                click.interactive=false;
                pageOut();
            }
        }
        function downHandler(e) {
            downPT.x=e.data.global.x;
            downPT.y=e.data.global.y;
        }
        function pageOut()
        {
            if(tw_click)
            {
                tw_click.stop();
            }
            sp_click.alpha=0;
            var event = new CustomEvent("Game_PageOut", {detail:5});
            var event = new CustomEvent("Game_PageIn", {detail:6});
            document.dispatchEvent(event);
            new TWEEN.Tween({scale:1,x:0,y:0,alpha:1})
                .to({scale:7,x:-2702,y:-1813,alpha:0},3000)
                .onUpdate(function () {
                    page.ui.scale.x=this.scale;
                    page.ui.scale.y=this.scale;
                    page.ui.alpha=this.alpha;
                    page.ui.x=this.x;
                    page.ui.y=this.y;
                })
                .onComplete(function () {
                    var tw=new TWEEN.Tween({alpha:0})
                        .to({alpha:0},0)
                        .onUpdate(function () {
                            page.ui.alpha=this.alpha;
                        })
                        .start();
                })
                .easing(TWEEN.Easing.Cubic.InOut)
                .start();
        }

    return page;
    }
}